class Solution(object):
    def findInteger(self, k, digit1, digit2):
        min_num = float('inf')
        now = {digit1, digit2}
        if digit1 > k and digit1 % k == 0:
            min_num = min(min_num, digit1)
        if digit2 > k and digit2 % k == 0:
            min_num = min(min_num, digit2)
        if min_num < float('inf'):
            return min_num
        for _ in range(9):
            next_st = set()
            for num in now:
                new_num = 10 * num + digit1
                if k < new_num <= 2 ** 31 - 1 and new_num % k == 0:
                    min_num = min(min_num, new_num)
                next_st.add(new_num)
                new_num = 10 * num + digit2
                if k < new_num <= 2 ** 31 - 1 and new_num % k == 0:
                    min_num = min(min_num, new_num)
                next_st.add(new_num)
            now = next_st
            if min_num < float('inf'):
                return min_num
        return -1